package projet.volleystat;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class StatPlayerActivity extends Activity {
	protected ListView listv;
	protected StatPlayersAdapter adapter;
	protected ArrayList<Player> players;
	protected ArrayList<Integer> playersid;
	protected DBInfo dbinfo;
	protected Button saveb;
	protected EditText filenameinput;
	protected String filename;
	protected String path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stat_player);

		saveb = (Button) findViewById(R.id.saveButton);

		Bundle bundle = getIntent().getExtras();
		playersid = bundle.getIntegerArrayList("selectedplayersarrayid");

		players = new ArrayList<Player>();

		dbinfo = new DBInfo(getApplicationContext());

		dbinfo.openDb();

		for (int i = 0; i < playersid.size(); i++) {
			try {
				players.add(dbinfo.getDb().getPlayerById(playersid.get(i)));
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
		}

		dbinfo.closeDb();

		listv = (ListView) findViewById(R.id.statsplayerlv);

		adapter = new StatPlayersAdapter(getApplicationContext(),
				R.layout.listitemstats, players);

		listv.setAdapter(adapter);

		saveb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						StatPlayerActivity.this);
				filenameinput = new EditText(StatPlayerActivity.this);
				filenameinput.setSingleLine(true);

				builder.setMessage(R.string.enterfilename);
				builder.setView(filenameinput);

				builder.setPositiveButton(R.string.save,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								filename = filenameinput.getText().toString()
										.trim();
								if (filename.length() != 0) {
									dbinfo = new DBInfo(getApplicationContext());
									try {
										dbinfo.loadAll();
										path = Environment
												.getExternalStorageDirectory()
												+ "/" + filename + ".html";
										dbinfo.exportToHTML(path,
												new ArrayList<String>(),
												players);

									
										Toast.makeText(StatPlayerActivity.this,
												"Enregistré dans : "+path,
												Toast.LENGTH_LONG).show();

									} catch (DatabaseException e) {
										Toast toast = Toast.makeText(
												StatPlayerActivity.this,
												R.string.dberror,
												Toast.LENGTH_LONG);
										toast.show();
										dialog.cancel();
										dbinfo.closeDb();
										e.printStackTrace();
									} catch (IOException e) {
										Toast toast = Toast.makeText(
												StatPlayerActivity.this,
												R.string.writeerror,
												Toast.LENGTH_LONG);
										toast.show();
										dialog.cancel();
										dbinfo.closeDb();
										e.printStackTrace();
									}

								} else {

									Toast toast = Toast.makeText(
											StatPlayerActivity.this,
											R.string.invalidname,
											Toast.LENGTH_LONG);
									toast.show();
									dialog.cancel();
								}
							}
						});

				builder.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});
				AlertDialog dialog = builder.create();
				dialog.show();

			}
		});
	}
}
